package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysHousehold;
import com.ruoyi.system.domain.SysHouseholdTask;
import com.ruoyi.system.domain.SysNucleicAcidTask;
import com.ruoyi.system.mapper.SysHouseholdMapper;
import com.ruoyi.system.service.ISysHouseholdService;
import com.ruoyi.system.service.ISysHouseholdTaskService;
import com.ruoyi.system.service.ISysNucleicAcidTaskService;
import com.ruoyi.system.service.ScanningCodeService;
import com.ruoyi.system.utils.AesTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ScanningCodeServiceImpl implements ScanningCodeService {

    @Autowired
    private ISysHouseholdService sysHouseholdService;

    @Autowired
    private ISysNucleicAcidTaskService sysNucleicAcidTaskService;

    @Autowired
    private ISysHouseholdTaskService sysHouseholdTaskService;


    @Override
    public void codeUrl(String url) throws Exception {
        // 钥匙
        String key = "aaaabbbbccccdddd";

        //AES解密
        String code = AesTool.deCode(url, key);

        // http://localhost/hslog?user=CDJEIFJ34&task=FJE33JF ,
        // 拼接要生成二维码的字符串
        //String codeUrl = "http://localhost/hslog?user="+enResult+"&task="+deResult;

        // 分割
        String str1 = code.split("user=")[1];  //CDJEIFJ34&task=FJE33JF

        String uid = str1.split("&")[0];

        String tid = str1.split("=")[1];

        /*住户核酸任务的状态修改为已经完成，然后设
        置检查时间。同时需要修改对应楼栋任务的完成人数*/

        // 修改楼栋核酸任务信息
        SysNucleicAcidTask sysNucleicAcidTask = sysNucleicAcidTaskService.selectSysNucleicAcidTaskById(Long.valueOf(tid));
        Integer total = sysNucleicAcidTask.getTotal();
        Integer complete = sysNucleicAcidTask.getComplete();
        String task = sysNucleicAcidTask.getTask();
        sysNucleicAcidTask.setTotal(total-1);
        sysNucleicAcidTask.setComplete(complete+1);
        sysNucleicAcidTaskService.updateSysNucleicAcidTask(sysNucleicAcidTask);

        // 修改住户信息
        SysHousehold household = sysHouseholdService.selectSysHouseholdById(Long.valueOf(uid));
        household.setState(1);
        household.setNote("完成核酸检测"+task);

        // 修改住户核酸任务信息
        SysHouseholdTask sysHouseholdTask = sysHouseholdTaskService.selectSysHouseholdTaskById(1l);//TODO
        sysHouseholdTask.setState(1);
        sysHouseholdTask.setDetectionTime(new Date());
        sysHouseholdTaskService.updateSysHouseholdTask(sysHouseholdTask);
    }
}
